package com.atguigu.srb.core.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.atguigu.srb.base.util.JwtUtils;
import com.atguigu.srb.common.util.Assert;
import com.atguigu.srb.common.result.ResponseEnum;
import com.atguigu.srb.common.util.MD5;
import com.atguigu.srb.core.enums.BorrowerStatusEnum;
import com.atguigu.srb.core.enums.UserBindEnum;
import com.atguigu.srb.core.mapper.UserAccountMapper;
import com.atguigu.srb.core.mapper.UserBindMapper;
import com.atguigu.srb.core.pojo.entity.UserBind;
import com.atguigu.srb.core.pojo.query.UserInfoQuery;
import com.atguigu.srb.core.pojo.vo.LoginVO;
import com.atguigu.srb.core.pojo.vo.RegisterVO;
import com.atguigu.srb.core.pojo.entity.UserAccount;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.mapper.UserInfoMapper;
import com.atguigu.srb.core.pojo.vo.UserInfoVO;
import com.atguigu.srb.core.service.UserBindService;
import com.atguigu.srb.core.service.UserInfoService;
import com.atguigu.srb.core.service.UserLoginRecordService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Map;

/**
 * <p>
 * 用户基本信息 服务实现类
 * </p>
 *
 * @author fan
 * @since 2021-11-22
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {
    @Resource
    private UserLoginRecordService userLoginRecordService;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private UserAccountMapper userAccountMapper;
    @Resource
    private UserBindMapper userBindMapper;
    @Override
    public void register(RegisterVO registerVO) {
        //通过查userInfo表中mobile字段数量来确定手机是否已注册
        String mobile = registerVO.getMobile();
        QueryWrapper<UserInfo> registerVOQueryWrapper = new QueryWrapper<>();
        registerVOQueryWrapper.eq("mobile",mobile);
        Integer integer = baseMapper.selectCount(registerVOQueryWrapper);
        Assert.isTrue(integer==0, ResponseEnum.MOBILE_EXIST_ERROR);
        //将数据存入数据库userInfo
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(registerVO,userInfo);
        userInfo.setNickName(mobile);
        userInfo.setId(mobile);

        userInfo.setPassword(MD5.encrypt(registerVO.getPassword()));
        baseMapper.insert(userInfo);

        //将数据存入userAccount
//        Long userInfoId = userInfo.getId();
        UserAccount userAccount = new UserAccount();
        userAccount.setUserId(mobile);
        userAccount.setAmount(new BigDecimal("0"));
        userAccount.setFreezeAmount(new BigDecimal("0"));
        userAccountMapper.insert(userAccount);

    }

    @Override
    public boolean checkCode(RegisterVO registerVO) {
        String codeA = (String) redisTemplate.opsForValue().get("srb:sms:code:" + registerVO.getMobile());
        String codeB = registerVO.getCode();

        Assert.equals(codeA,codeB,ResponseEnum.CODE_ERROR);
        return true;
    }

    @Override
    public UserInfoVO login(LoginVO loginVO,String ipRemote) {

        String mobile = loginVO.getMobile();
        Assert.notNull(mobile,ResponseEnum.MOBILE_NULL_ERROR);
        String userType = loginVO.getUserType();
        String password = MD5.encrypt(loginVO.getPassword());
        Assert.notNull(password,ResponseEnum.PASSWORD_NULL_ERROR);

        //存入登录日志
        userLoginRecordService.setUserLoginRecord(mobile, ipRemote);

        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper
                .eq("mobile",mobile)
                .eq("user_type",userType);
        UserInfo userInfo = baseMapper.selectOne(userInfoQueryWrapper);
        Assert.notNull(userInfo,ResponseEnum.LOGIN_MOBILE_ERROR);
        String password1 = userInfo.getPassword();
        Assert.equals(password1,password,ResponseEnum.LOGIN_PASSWORD_ERROR);
        UserInfoVO userInfoVO = new UserInfoVO();

        String token = JwtUtils.createToken(userInfo.getMobile(), userInfo.getName());

        BeanUtils.copyProperties(userInfo,userInfoVO);

        userInfoVO.setToken(token);

        return userInfoVO;
    }

    @Override
    public Page<UserInfo> pageList(Long page, Long limit, UserInfoQuery userInfoQuery) {
        Page<UserInfo> userInfoPage = new Page<>(page, limit);
        String mobile = userInfoQuery.getMobile();
        Integer status = userInfoQuery.getStatus();
        Integer userType = userInfoQuery.getUserType();
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.like(!StringUtils.isEmpty(mobile), "mobile",mobile)
                .like(null!=status,"status",status)
                .eq(null!=userType,"user_type",userType);
        userInfoPage.setSize(limit);
        userInfoPage.setCurrent(page);
        Page<UserInfo> pageList = baseMapper.selectPage(userInfoPage, userInfoQueryWrapper);
        return pageList;
    }

    @Override
    public Integer lockUserInfoStatus(String id, Integer status) {
        UserInfo userInfo = new UserInfo();
        userInfo.setStatus(status);
        userInfo.setId(id);
        int i = baseMapper.updateById(userInfo);
        UserInfo userInfo1 = baseMapper.selectById(i);
        Integer statusChange = userInfo1.getStatus();
        return statusChange;
    }

    @Override
    public boolean checkMobile(String mobile) {
        boolean b = false;
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.eq("mobile",mobile);
        Integer integer = baseMapper.selectCount(userInfoQueryWrapper);
        if(integer>0){
            b = true;
        }
        return b;

    }

    @Override
    public void updateUserInfo(Map<String, Object> stringObjectMap) {
        String userId = (String) stringObjectMap.get("agentUserId");
        String bindCode = (String) stringObjectMap.get("bindCode");

        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.eq("id",userId);
        UserInfo userInfo = baseMapper.selectOne(userInfoQueryWrapper);

        QueryWrapper<UserBind> userBindQueryWrapper = new QueryWrapper<>();
        userBindQueryWrapper.eq("userId", userId);
        UserBind userBind = userBindMapper.selectOne(userBindQueryWrapper);

        userInfo.setBindCode(bindCode);
        userInfo.setName(userBind.getName());
        userInfo.setIdCard(userBind.getIdCard());
        userInfo.setBindStatus(UserBindEnum.BIND_OK.getStatus());
        baseMapper.update(userInfo,userInfoQueryWrapper);
//        baseMapper.updateById(userInfo);

    }

    @Override
    public void setStatus(String userId) {
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.eq("id",userId);
        UserInfo userInfo = new UserInfo();
        userInfo.setBorrowAuthStatus(BorrowerStatusEnum.AUTH_RUN.getStatus());
        baseMapper.update(userInfo,userInfoQueryWrapper);

    }

    @Override
    public String getMobileByBindCode(String bindCode) {
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.eq("bind_code", bindCode);
        UserInfo userInfo = baseMapper.selectOne(userInfoQueryWrapper);
        return userInfo.getMobile();
    }

    @Override
    public String getBindCodeByUserId(String userId) {
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.eq("id",userId);
        UserInfo userInfo = baseMapper.selectOne(userInfoQueryWrapper);
        String bindCode = userInfo.getBindCode();
        return bindCode;
    }
}
